package cn.qingyun.gis.modules.enterprise.controller;

import cn.hutool.crypto.SecureUtil;
import cn.qingyun.gis.cache.common.facade.CacheFacade;
import cn.qingyun.gis.cache.constant.CacheConstant;
import cn.qingyun.gis.common.Result;
import cn.qingyun.gis.modules.enterprise.entity.Permissions;
import cn.qingyun.gis.modules.enterprise.service.IPermissionsService;
import cn.qingyun.gis.modules.enterprise.util.RoleTool;
import cn.qingyun.gis.modules.init.system.constant.CommonConstant;
import cn.qingyun.gis.modules.init.system.entity.SysUser;
import cn.qingyun.gis.modules.init.system.service.IBaseCommonService;
import cn.qingyun.gis.modules.init.system.service.ISysUserService;
import cn.qingyun.gis.modules.init.system.sso.request.SsoLoginRequest;
import cn.qingyun.gis.modules.init.system.sso.request.SsoLoginResponse;
import cn.qingyun.gis.modules.init.system.vo.LoginUser;
import cn.qingyun.gis.modules.init.utils.JwtUtil;
import cn.qingyun.gis.modules.init.utils.MD5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/sso")
public class MeltGridSsoLoginController {
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    private IBaseCommonService iBaseCommonService;

    @Autowired
    private IPermissionsService iPermissionsService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody @Validated SsoLoginRequest request) {
        List<Permissions> permissions = iPermissionsService.getGridByAccount(request.getCrmAccount());
        iPermissionsService.syncPermissions(permissions, request.getPhoneNumber(),request.getCrmAccount());
        Permissions channelPermissions = iPermissionsService.isValidCrmAccount(request.getCrmAccount());
        // 保证自动同步系统用户， 前提是有guest 权限
        SysUser sysUser = iSysUserService.ensureUserIsValidWithGuestAuth(request.getCrmAccount(), request.getPhoneNumber(), channelPermissions.getUsername());
        iSysUserService.editUserWithRole(sysUser, RoleTool.getRoleByManagerType(channelPermissions.getManagerType()).getId());
        String token = JwtUtil.sign(sysUser.getUsername(), MD5Util.MD5Encode(sysUser.getPassword(), StandardCharsets.UTF_8.name()));
        CacheFacade.set(CommonConstant.PREFIX_USER_TOKEN + token, token, JwtUtil.EXPIRE_TIME / 1000);
        CacheFacade.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000);

        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(sysUser, loginUser);
        loginUser.setPassword(SecureUtil.md5(sysUser.getPassword()));
        CacheFacade.set(CacheConstant.SYS_USERS_CACHE_JWT + ":" + token, loginUser, JwtUtil.EXPIRE_TIME / 1000);
        CacheFacade.expire(CacheConstant.SYS_USERS_CACHE_JWT + ":" + token, JwtUtil.EXPIRE_TIME / 1000);

        iBaseCommonService.addLog("用户名: " + sysUser.getUsername() + ",登录成功！", CommonConstant.LOG_TYPE_1, null, loginUser);

        return Result.okData(SsoLoginResponse.builder().token(token).build());
    }
}
